package cn.school.controller.workflow;

import cn.school.common.api.CommonPage;
import cn.school.common.api.CommonResult;
import cn.school.common.exception.ApiException;
import cn.school.workflow.service.QuitApplyService;
import cn.school.workflow.vo.QuitApplyVO;
import cn.school.workflow.vo.WorkFlowVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/quitApply")
public class QuitApplyController {

    @Autowired
    private QuitApplyService quitApplyService;

    /**
     * 分页查询离职申请信息
     * @param vo
     * @return
     * @throws ApiException
     */
    @PostMapping("/findQuitApplyPageList")
    public CommonResult<CommonPage<QuitApplyVO>> findQuitApplyPageList(@RequestBody QuitApplyVO vo) throws ApiException {
        IPage<QuitApplyVO> pageList =  quitApplyService.findQuitApplyPageList(vo);
        return  CommonResult.success(CommonPage.restPage(pageList));
    }

    @PostMapping("/addOrUpdateQuitApply")
    @PreAuthorize("@ss.hasPermi('quit:apply:create')")
    public CommonResult<Integer> addOrUpdateQuitApply(@RequestBody QuitApplyVO vo) throws ApiException {
        int num =  quitApplyService.addOrUpdateQuitApply(vo);
        if(num==-1){
            return  CommonResult.failed("有离职流程正在处理，不能重复提交");
        }
        if(num>0){
            return CommonResult.success();
        }
        return  CommonResult.failed("离职流程创建失败");
    }

    /**
     * 撤销离职流程
     * @param vo
     * @return
     * @throws ApiException
     */
    @PostMapping("/cancelQuitApply")
    @PreAuthorize("@ss.hasPermi('quit:apply:cancel')")
    public CommonResult<Integer> cancelQuitApply(@RequestBody WorkFlowVO vo) throws ApiException {
        boolean num =  quitApplyService.deleteQuitApplyById(vo);
        if(num){
            return CommonResult.success();
        }
        return  CommonResult.failed("离职流程创建失败");
    }

    @GetMapping("/findQuitApplyDetail/{processInstanceId}")
    public CommonResult<QuitApplyVO> findQuitApplyDetail(@PathVariable("processInstanceId") Long processInstanceId) throws ApiException {
        QuitApplyVO quitApplyVO =  quitApplyService.findQuitApplyDetail(processInstanceId);
        return CommonResult.success(quitApplyVO);
    }

}
